package cn.edu.nju.ws.sview.wyge.servlets;

import java.io.IOException;
import java.net.URLDecoder;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.JSONValue;

import cn.edu.nju.ws.sview.wyge.relations.adataset.bookmark.DatasetBookmark;
import cn.edu.nju.ws.sview.wyge.relations.datasets.DatasetItem;

public class BookmarkShow extends HttpServlet {

	/**
	 * The doGet method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		this.doPost(request, response);
		
	}

	/**
	 * The doPost method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to post.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		response.setCharacterEncoding("utf-8");
		String option = null;
		String json = null;
		if(request.getParameter("info") != null){
			json = request.getParameter("info");
			option = "info";					
		}
		if(option.equals("info")){
			String str = URLDecoder.decode(json, "UTF-8");
			JSONObject regtJSON = (JSONObject) JSONValue.parse(str);
			String type = (String) regtJSON.get("type");
			String lang = (String) regtJSON.get("lang");
			int start = ((Long) regtJSON.get("start")).intValue();
			int length = ((Long) regtJSON.get("length")).intValue();
			ArrayList<DatasetItem> array = null;
			if(type.contentEquals("mineBookmark")){
				  String userName = (String) regtJSON.get("user");
				  
				  array = DatasetBookmark.getBookmarkItems(userName, lang, start, length);
				  System.out.println("equal!");
			}else{
				System.out.println("not!");
			}
			JSONArray elements = new JSONArray();
//			System.out.println(str+"\tarray:"+array);
			for(DatasetItem item : array){
				JSONObject jsob = new JSONObject();
				jsob.put("genre", item.getGenre());
				jsob.put("id", item.getId());
//				jsob.put("name", item.getName());
				jsob.put("name", item.getShowName());
				elements.add(jsob);
			}
			JSONObject result = new JSONObject();
			result.put("result", elements);
			response.getWriter().print(result);
		}
		
	}

}
